package cn.service.impl;

import java.util.List;

import cn.dao.IUserDao;
import cn.dao.impl.UserDao;
import cn.entity.Page;
import cn.entity.User;
import cn.service.IUserService;

public class UserService implements IUserService {
	private IUserDao userDao = new UserDao();

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public void add(User user) throws Exception {
		userDao.add(user);
	}

	public void update(User user) throws Exception {
		userDao.update(user);
	}

	public void delete(int u_id) throws Exception {
		userDao.delete(u_id);
	}

	public User findOne(User user) throws Exception {
		return userDao.findOne(user);
	}

	public Page<?> showAllRecord(int currPageNo, int perPageSize)
			throws Exception {
		// 获取分页对象
		Page<User> page = new Page<User>();
		// 设置 每页记录条数
		page.setPerPageSize(perPageSize);
		// 获取记录总数
		int allRecordNo = userDao.getAllRecord(null);
		// 设置总记录数
		page.setAllRecordNo(allRecordNo);

		// 设置总页数
		int allPageNo;
		if (allRecordNo % page.getPerPageSize() == 0) {
			allPageNo = allRecordNo / page.getPerPageSize();
		} else {
			allPageNo = allRecordNo / page.getPerPageSize() + 1;
		}
		page.setAllPageNo(allPageNo);

		// 判断 页数 非法情况 <0 或 > 总页数 是处理
		if (currPageNo <= 0) {
			page.setCurrPageNo(1);
		} else if (currPageNo > page.getAllPageNo()) {
			page.setCurrPageNo(page.getAllPageNo());
		} else {
			page.setCurrPageNo(currPageNo);

		}
		// 封装该本显示的内容

		/*
		 * oracle int start = (page.getCurrPageNo()-1) * page.getPerPageSize() ;
		 * int end = page.getCurrPageNo() * page.getPerPageSize() +1
		 */
		/*
		 * ,mysql
		 */
		int start = (page.getCurrPageNo() - 1) * page.getPerPageSize();// 起始行数
		int end = page.getPerPageSize();
		List<User> findAllRecord = userDao.findAllRecord(start, end);
		page.settList(findAllRecord);
		return page;
	}

	public Page<?> showUsersByCondition(int currPageNo, int perPageSize,
			User user) throws Exception {
		// 获取分页对象
		Page<User> page = new Page<User>();
		// 设置 每页记录条数
		page.setPerPageSize(perPageSize);
		// 获取记录总数
		int allRecordNo = userDao.getAllRecord(user);
		// 设置总记录数
		page.setAllRecordNo(allRecordNo);

		// 设置总页数
		int allPageNo;
		if (allRecordNo % page.getPerPageSize() == 0) {
			allPageNo = allRecordNo / page.getPerPageSize();
		} else {
			allPageNo = allRecordNo / page.getPerPageSize() + 1;
		}
		page.setAllPageNo(allPageNo);

		// 判断 页数 非法情况 <0 或 > 总页数 是处理
		if (currPageNo <=0) {
			page.setCurrPageNo(1);
		} else if (currPageNo > page.getAllPageNo()) {
			page.setCurrPageNo(page.getAllPageNo());
		} else {
			page.setCurrPageNo(currPageNo);

		}
		// 封装该本显示的内容

		/*
		 * oracle int start = (page.getCurrPageNo()-1) * page.getPerPageSize() ;
		 * int end = page.getCurrPageNo() * page.getPerPageSize() +1
		 */
		/*
		 * ,mysql
		 */
		int start = (page.getCurrPageNo() - 1) * page.getPerPageSize();// 起始行数
		int end = page.getPerPageSize();
		List<User> findByCondition = userDao.findByCondition(start, end, user);
		page.settList(findByCondition);
		return page;
	}

	public Page<?> showUsersByTime(int currPageNo, int perPageSize,
			String starTime, String endTime) throws Exception {
		// 获取分页对象
		Page<User> page = new Page<User>();
		// 设置 每页记录条数
		page.setPerPageSize(perPageSize);
		// 获取记录总数
		int allRecordNo = userDao.getAllRecord(null);
		// 设置总记录数
		page.setAllRecordNo(allRecordNo);

		// 设置总页数
		int allPageNo;
		if (allRecordNo % page.getPerPageSize() == 0) {
			allPageNo = allRecordNo / page.getPerPageSize();
		} else {
			allPageNo = allRecordNo / page.getPerPageSize() + 1;
		}
		page.setAllPageNo(allPageNo);

		// 判断 页数 非法情况 <0 或 > 总页数 是处理
		if (currPageNo <=0) {
			page.setCurrPageNo(1);
		} else if (currPageNo > page.getAllPageNo()) {
			page.setCurrPageNo(page.getAllPageNo());
		} else {
			page.setCurrPageNo(currPageNo);

		}
		// 封装该本显示的内容

		/*
		 * oracle int start = (page.getCurrPageNo()-1) * page.getPerPageSize() ;
		 * int end = page.getCurrPageNo() * page.getPerPageSize() +1
		 */
		/*
		 * ,mysql
		 */
		int start = (page.getCurrPageNo() - 1) * page.getPerPageSize();// 起始行数
		int end = page.getPerPageSize();
		List<User> findByCreateTime = userDao.findByCreateTime(start, end,
				starTime, endTime);
		page.settList(findByCreateTime);
		return page;
	}

}
